﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Honestysoft.Financing.WebUI
{
    public class zTreeUtil
    {
        public class zTreeNode
        {
            public string id { get; set; }
            public string name { get; set; }
            public bool isParent { get; set; }
            public string icon { get; set; }
            public string target { get; set; }
            public string url { get; set; }
            public string nodeType { get; set; }

            public string ext1 { get; set; }
            public string ext2 { get; set; }
            public string ext3 { get; set; }

            public override string ToString()
            {
                return ToJsonString();
            }

            private string ToJsonString()
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("{");
                sb.AppendFormat("\"id\":\"{0}\",", id);
                sb.AppendFormat("\"name\":\"{0}\",",name.Replace("\\", "\\\\").Replace("\'", "\\\'").Replace("\t", " ").Replace("\r", " ").Replace("\n", "<br/>").Replace("\"", "'"));
                sb.AppendFormat("\"target\":\"{0}\",", target);
                sb.AppendFormat("\"icon\":\"{0}\",", icon);
                sb.AppendFormat("\"url\":\"{0}\",", url);
                sb.AppendFormat("\"nodeType\":\"{0}\",", nodeType);
                sb.AppendFormat("\"ext1\":\"{0}\",", ext1);
                sb.AppendFormat("\"ext2\":\"{0}\",", ext2);
                sb.AppendFormat("\"ext3\":\"{0}\",", ext3);
                sb.AppendFormat("\"isParent\":\"{0}\"", isParent ? "true" : "false");
                sb.Append("}");
                return sb.ToString();
            }
        }

        public static string GetJsonString(List<zTreeNode> lsNodes)
        {
            if (lsNodes == null || lsNodes.Count == 0) { return ""; }
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            foreach (zTreeNode item in lsNodes)
            {
                sb.Append(item.ToString());
                sb.Append(",");
            }
            if (sb.ToString().EndsWith(","))
            {
                sb.Remove(sb.Length - 1, 1);
            }
            sb.Append("]");
            return sb.ToString();
        }
    }
}